
// 定义一个trait
trait Summary {
    fn summary(&self) -> String;    // 只是定义了方法签名
}

struct Article {
    body: String,
    title: String,
}

// 定义Article上的Summary trait实现
impl Summary for Article {
    fn summary(&self) -> String {
        return format!("Summary of article \"{}\": ...", self.title);
    }
}

// 为Vec<i32>实现summary trait
impl Summary for Vec<i32> {
    fn summary(&self) -> String {
        format!("I am a vector with len = {}", self.len())
    }
}

fn main() {
    let article = Article {
        body: String::from("body"),
        title: String::from("I am learning Rust!"),
    };
    println!("{}", article.summary());

    let mut v = Vec::<i32>::new();
    v.push(1);
    v.push(2);
    println!("{}", v.summary());
}
